package com.xkdx.caipiao;

import java.util.HashMap;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.user.MainInfoSetAction;
import com.xkdx.caipiao.presistence.user.MainInfoSetModule;
import com.xkdx.caipiao.presistence.user.MainInfoSetPresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;

public class CompleteUserInfoActivity extends ThreadActivity {
	MainInfoSetAction action;
	MainInfoSetModule module;
	MainInfoSetPresistence presistence;
	EditText rel_name, nick_name, id, phone,name;
	Button sure;
	String relName, nickName, ID, pho;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.complete_user_info_caipiao);
		init();

	}

	private void init() {
		sure = (Button) findViewById(R.id.sure);
		rel_name = (EditText) findViewById(R.id.name);
		nick_name = (EditText) findViewById(R.id.nick_name);
		id = (EditText) findViewById(R.id.ID);
		phone = (EditText) findViewById(R.id.phone_number);
		name = (EditText) findViewById(R.id.name);
		sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(TextUtils.isEmpty(nick_name.getText().toString())){
					Toast.makeText(CompleteUserInfoActivity.this, "昵称不能为空", 1).show();
					return;
				}
				if(TextUtils.isEmpty(name.getText().toString())){
					Toast.makeText(CompleteUserInfoActivity.this, "姓名不能为空", 1).show();
					return;
				}
				if(TextUtils.isEmpty(id.getText().toString())){
					Toast.makeText(CompleteUserInfoActivity.this, "身份证号不能为空", 1).show();
					return;
				}else if (id.getText().toString().length() < 18) {
					Toast.makeText(CompleteUserInfoActivity.this, "请输入18位身份证号", 1).show();
					return;
				}
				if(TextUtils.isEmpty(phone.getText().toString())){
					Toast.makeText(CompleteUserInfoActivity.this, "手机号不能为空", 1).show();
					return;
				}else if (phone.getText().toString().length() < 11) {
					Toast.makeText(CompleteUserInfoActivity.this, "请输入正确的手机号", 1).show();
					return;
				}
				startThread();

			}
		});

	}

	public void onBackClick(View view) {
		finish();
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {

		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			if (module.isSuccess) {
				Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
//				SharePrefenceUtil su = new SharePrefenceUtil(this,
//						SharePrefenceUtil.USERINFO);
//				LoginInfo info = su.getUserInfo();
//				info.setUser_nickname(nickName);
//				info.setVerify("1");
//				su.saveObject(SharePrefenceUtil.USERINFO, info);

				UserSharePrefence1 usrSP = new UserSharePrefence1(this, com.xkdx.guangguang.module.statics.IConstants.SP_USER_NEW);
				usrSP.setUserNickName(nickName);
				usrSP.setVerify("1");
				finish();
			}
		} else {
			Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
//		LoginInfo info = getLoginInfo();
		relName = rel_name.getText().toString();
		nickName = nick_name.getText().toString();
		ID = id.getText().toString();
		pho = phone.getText().toString();
		if (relName.equals("") || nickName.equals("") || ID.equals("")
				|| pho.equals("")) {
			Toast.makeText(this, "请补全身份信息，以方便领取奖金", Toast.LENGTH_SHORT).show();
			return;
		}
		String md5 = usrSP.getId()+usrSP.getLogintoken()+IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new MainInfoSetAction(usrSP.getId(), usrSP.getLogintoken(), ID,
				"1", relName, nickName, pho,signcode);

		module = new MainInfoSetModule();
		presistence = new MainInfoSetPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

}
